<template>
  <div id="register">
    <div class="register-container">
      <h3 class="title">
        {{ $store.state.USER == 1 ? "托运人注册" : "承运人注册" }}
      </h3>
      <!-- 注册 -->
      <el-form
        label-position="left"
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
      >
        <el-form-item prop="name">
          <el-input
            prefix-icon="el-icon-office-building"
            type="text"
            v-model="ruleForm.name"
            placeholder="请输入企业名称"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="phone">
          <el-input
            prefix-icon="el-icon-mobile-phone"
            type="text"
            v-model="ruleForm.phone"
            placeholder="请输入手机号"
          >
          </el-input>
        </el-form-item>
        <slideVerify @sliderStatus="sliderStatus" />
        <el-form-item prop="code">
          <el-input
            prefix-icon="el-icon-message"
            placeholder="短信验证码"
            v-model="ruleForm.code"
          >
            <template slot="append">
              <button
                @click="resetTime()"
                style="background: none;line-height: 48px;cursor: pointer;color: $blue;"
                :disabled="disabled"
              >
                {{ btnText }}
              </button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password1">
          <el-input
            prefix-icon="el-icon-lock"
            type="password"
            v-model="ruleForm.password1"
            placeholder="请输入密码"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="password2">
          <el-input
            prefix-icon="el-icon-lock"
            type="password"
            v-model="ruleForm.password2"
            placeholder="请再次输入密码"
          >
          </el-input>
        </el-form-item>
        <el-form-item style="width:100%;">
          <el-button
            type="primary"
            style="width:100%;"
            @click="registerTo('ruleForm')"
            >注册</el-button
          >
        </el-form-item>

        <div style="width:100%;margin-top: -10px;" class="fb">
          <div>
            <el-checkbox class="remember" v-model="rememberpwd"
              >已同意并阅读
            </el-checkbox>
            <span style="color: $blue;font-size: 14px;cursor: pointer;"
              >&nbsp;平台协议</span
            >
          </div>
          <div>
            <router-link tag="span" :to="{ path: '/login' }" class="regist"
              >已有帐号&emsp;
              <span style="color: $blue;">直接登录</span>
            </router-link>
          </div>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import slideVerify from "@/components/slideVerify.vue";
export default {
  name: "register",
  components: {
    slideVerify
  },
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.password1) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      toggle: false,
      disabled: false,
      status: false,
      btnText: "发送验证码",
      rememberpwd: "",
      ruleForm: {
        //phone和password默认为空
        name: "",
        phone: "",
        password1: "",
        password2: "",
        code: ""
      },
      //rules前端验证
      rules: {
        name: [{ required: true, message: "请输入企业名称", trigger: "blur" }],
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            pattern: /^(1[3-9])\d{9}$/,
            message: "输入的手机号不合法",
            trigger: "blur"
          }
        ],
        password1: [{ required: true, message: "请输入密码", trigger: "blur" }],
        password2: [{ validator: validatePass, trigger: "blur" }],
        code: [{ required: true, message: "请输入验证码", trigger: "blur" }]
      }
    };
  },
  created() {},
  mounted() {},
  methods: {
    // 注册
    registerTo(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (!this.status) {
            this.$message({ message: "请滑动完成验证", type: "error" });
          } else {
            apiRegisty({
              type: 1,
              CompanyName: this.ruleForm.name,
              phone: this.ruleForm.phone,
              password: this.ruleForm.password1
            }).then(res => {
              console.log(res);
            });
          }
        } else {
          this.$message({ message: "请完善填写内容", type: "error" });
          return false;
        }
      });
      // apiRegisty({
      //               type: 1,
      //               CompanyName: this.ruleForm.name,
      //               phone: this.ruleForm.phone,
      //               password: this.ruleForm.password1
      //             }).then(res => {
      //               console.log(res);
      //             });
      // this.$router.push("/login");
      // this.$message({message: "注册成功",type: "success"});
    },
    // 接受滑动验证码状态
    sliderStatus(status) {
      this.status = status;
    },
    resetTime() {
      var second = 60;
      var timer = null;
      timer = setInterval(() => {
        second -= 1;
        if (second > 0) {
          this.disabled = true;
          this.btnText = second + "S";
        } else {
          clearInterval(timer);
          this.disabled = false;
          this.btnText = "重新获取";
        }
      }, 1000);
    }
  }
};
</script>
<style lang="scss">
#register {
  .el-input__inner {
    line-height: 50px;
    height: 50px;
  }
  .el-form-item {
    margin-bottom: 30px;
  }
  .el-button {
    height: 50px;
    font-size: 20px;
    border-radius: 10px;
  }
  .el-input-group--append {
    height: 50px;
    margin-top: 30px;
  }
  .el-input-group__append {
    width: 68px;
    text-align: center;
    cursor: pointer;
    background-color: #fff;
    color: $blue;
  }
}
</style>

<style lang="scss" scoped>
#register {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  background-image: url("../assets/img/homebg2.jpg");
  background-repeat: no-repeat;
  background-position: center center;
  // background-size: 100%;
  position: relative;
  .register-container {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 4px;
    width: 650px;
    padding: 40px 125px 55px;
    background: #fff;
    box-sizing: border-box;
    // border: 1px solid #eaeaea;
    text-align: left;
    box-shadow: 0px 1px 25px 0px rgba(192, 192, 192, 0.75);
    .title {
      font-size: 18px;
      font-family: Microsoft YaHei;
      font-weight: bold;
      line-height: 68px;
      text-align: center;
      border-bottom: 1px solid rgba(0, 0, 0, 0.1);
      margin-bottom: 36px;
    }
    .regist {
      cursor: pointer;
      font-size: 14px;
    }
  }
}
</style>
